package com.mantou.proxyservice.scheduler;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.mantou.proxyservice.clean.Cleaner;
import com.mantou.proxyservice.collection.Collecter;
import com.mantou.proxyservice.proxeservice.entity.Proxy;
import com.mantou.proxyservice.proxeservice.service.ProxyService;

public class CollecterTask implements Runnable {

	@Autowired
	private ProxyService proxyService;
	
	static Logger logger = Logger.getLogger(CollecterTask.class);
	
	private ThreadPoolTaskExecutor executor;
	
	public ThreadPoolTaskExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(ThreadPoolTaskExecutor executor) {
		this.executor = executor;
	}

	public static List<Collecter> getCollecters() {
		return collecters;
	}


	static List<Collecter> collecters = null;
	static{
		try {
			logger.info("start collecter");
			collecters = Collecter.buildfromSource("/handmaper.xml");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		/*int i =0;
		if(i==0)
			return ;*/
		
		try {
			for(Collecter collecter:collecters){
				executor.execute(new WebsiteCollect(collecter));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
	}

	private class WebsiteCollect implements Runnable{

		private Collecter collecter;
		
		public WebsiteCollect(Collecter collecter) {
			super();
			this.collecter = collecter;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			List<Proxy> draftproxys = collecter.newProxy();
			Cleaner.filter(draftproxys);
			proxyService.save(draftproxys);
			
		}
		
	}
}
